<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head th:replace="topbar::static">
</head>

<body>
<!--引用公用的导航栏-->
<nav th:replace="topbar::topbar"></nav>
<div class="container-fluid">
    <div class="row">
        <div th:replace="topbar::sidebar(activeUri='nsinstance.html')"></div>
        <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
            <h1 class="page-header">5GC MANO</h1>
            <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#newNsInstance">
                新增NsInstance
            </button>
            <!--<h2 class="sub-header">Section title</h2>-->
            <div class="table-responsive">
                <table class="table table-striped">
                    <thead>
                    <tr>
                        <th>id</th>
                        <th>nsInstanceName</th>
                        <th>nsInstanceDescription</th>
                        <th>nsdId</th>
                        <th>vnfInstance</th>
                        <th>nsState</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    <tr th:each="nsInstance:${nsInstances}">
                        <td th:text="${nsInstance.id}"></td>
                        <td th:text="${nsInstance.nsInstanceName}"></td>
                        <td th:text="${nsInstance.nsInstanceDescription}"></td>
                        <td th:text="${nsInstance.nsdId}"></td>
                        <td th:text="${nsInstance.vnfInstance}"></td>
                        <td th:text="${nsInstance.nsState}"></td>
                        <td>
                            <button type="button" class="btn btn-default" aria-label="Left Align"
                                    th:onclick="|instantiate(${nsInstance.id})|">
                                实例化
                            </button>
                            <button type="button" class="btn btn-primary" aria-label="Left Align" data-toggle="modal"
                                    data-target="#scaleNsInstance"
                                    th:onclick="|fillScaleNsInstanceId(${nsInstance.id})|">
                                伸缩
                            </button>
                            <button type="button" class="btn btn-danger" aria-label="Left Align"
                                    th:onclick="|terminate(${nsInstance.id})|">
                                终止
                            </button>
                            <button type="button" class="btn btn-danger" aria-label="Left Align"
                                    th:onclick="|deleteNsInstance(${nsInstance.id})|">
                                <span class="glyphicon glyphicon-remove"></span>
                            </button>
                        </td>
                    </tr>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<!--模态框，用于新增ns实例-->
<div id="newNsInstance" class="modal fade bs-example-modal-lg" tabindex="-1" role="dialog"
     aria-labelledby="myLargeModalLabel">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title" id="myModalLabel">新增NS实例</h4>
            </div>
            <div class="modal-body">
                <table class="table table-striped">
                    <div class="table-responsive">
                        <thead>
                        <tr>
                            <th>nsInstanceName</th>
                            <th>nsInstanceDescription</th>
                            <th>nsdId</th>
                        </tr>
                        </thead>
                        <tbody>
                        <tr>
                            <td><input type="text" class="form-control" id="nsInstanceName"></td>
                            <td><input type="text" class="form-control" id="nsInstanceDescription"></td>
                            <td>
                                <select id="nsdId" class="selectpicker">
                                    <option class="form-control" th:each="nsd:${nsds}" th:value="${nsd.nsdId}">
                                        <span th:text="${nsd.nsdName}"></span>:
                                        <span th:text="${nsd.version}"></span>
                                    </option>
                                </select>
                            </td>
                        </tr>
                        </tbody>
                    </div>
                </table>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" onclick="create()">保存</button>
            </div>
        </div>
    </div>
</div>

<!--模态框，用于伸缩NS实例-->
<div id="scaleNsInstance" class="modal fade bs-example-modal-lg" tabindex="-1" role="dialog"
     aria-labelledby="myLargeModalLabel">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title">伸缩NS实例</h4>
            </div>
            <div class="modal-body">
                <table class="table table-striped">
                    <div class="table-responsive">
                        <thead>
                        <tr>
                            <th>nsInstanceId</th>
                            <th>scaleType</th>
                            <th>scaleVnfData(VnfInstanceId+实例数目，逗号分隔，可以多组)</th>
                        </tr>
                        </thead>
                        <tbody>
                        <tr>
                            <td><input type="text" class="form-control" id="nsInstanceId" readonly="readonly"></td>
                            <td>
                                <select id="scaleType" class="selectpicker">
                                    <option class="form-control">SCALE_OUT</option>
                                    <option class="form-control">SCALE_IN</option>
                                </select>
                            </td>
                            <td><input type="text" class="form-control" id="scaleVnfData"></td>
                        </tr>
                        </tbody>
                    </div>
                </table>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" onclick="scale()">保存</button>
            </div>
        </div>
    </div>
</div>


</body>

<script>
    var baseUrl = "http://"+hostIp+":8002/ns_instances/";

    function create() {
        var url = baseUrl + "create";
        $.ajax({
            url: url,
            type: "POST",
            data: {
                "nsInstanceName": $("#nsInstanceName").val(),
                "nsInstanceDescription": $("#nsInstanceDescription").val(),
                "nsdId": $("#nsdId").val()
            },
            complete: function (id) {
                alert("ns实例创建成功");
                reload();
            }
        });
    }

    function deleteNsInstance(id) {
        //异步删除nsd
        var url = baseUrl + "delete";
        $.ajax({
            url: url,
            type: "POST",
            data: {
                "id": id
            },
            complete: function (id) {
                alert("ns实例删除成功");
                reload();
            }
        });
    }

    function instantiate(id) {
        //异步实例化nsd
        var url = baseUrl + "instantiate";
        $.ajax({
            url: url,
            type: "POST",
            data: {
                "id": id
            },
            complete: function (id) {
                alert("ns实例化成功");
                reload();
            }
        });
    }

    function scale() {
        var url = baseUrl + "scale";
        $.ajax({
            url: url,
            type: "POST",
            data: {
                "nsInstanceId": $("#nsInstanceId").val(),
                "scaleType": $("#scaleType").val(),
                "scaleVnfData": $("#scaleVnfData").val()
            },
            complete: function (id) {
                alert("ns实例伸缩成功");
                reload();
            }
        });
    }

    function terminate(id) {
        var url = baseUrl + "terminate";
        $.ajax({
            url: url,
            type: "POST",
            data: {
                "id": id
            },
            complete: function (id) {
                alert("ns中止成功");
                reload();
            }
        });
    }

    function fillScaleNsInstanceId(id) {
        $("#nsInstanceId").val(id);
    }

    function reload() {
        $("#newNsInstance").modal('hide');
        window.location.reload();
    }
</script>
</html>
